﻿using System;
using System.Data;
using DataAccess;
using System.Globalization;

namespace Business
{
    public class BuMain
    {
        public static bool createDatabase(string url,string companyid)
        {
            DaMain main = new DaMain();
            return main.createDatabase(url,companyid);
        }
        public static string selReportedTime(DateTime date, string email, string companyid)
        {
            DaMain main = new DaMain();
            DataTable data = main.selReportedTime(date, email, companyid);
            string result = String.Empty;
            result += "<table>";
            result += "<tr><th class='header'>Thời gian làm việc theo 4 tuần qua</th><tr>";
            result += "<tr><table class='inbox'>";
            result += "<tr><th align='center' style='color:#375B91;background-color:#F3F9FF;'>Loại thời giờ</th><th align='center' style='color:#375B91;background-color:#F3F9FF;'>Cách 3 tuần</th><th align='center' style='color:#375B91;background-color:#F3F9FF;'>Cách 2 tuần</th><th align='center' style='color:#375B91;background-color:#F3F9FF;'>Tuần trước</th><th align='center' style='color:#375B91;background-color:#F3F9FF;'>Tuần này</th></tr>";

            int numOfRow = data.Rows.Count;
            DataRow row;
            int total3h = 0;
            int total3m = 0;
            int total2h = 0;
            int total2m = 0;
            int total1h = 0;
            int total1m = 0;
            int totalh = 0;
            int totalm = 0;

            for (int i = 0; i < numOfRow; i++)
            {
                row = data.Rows[i];
                int hour3 = Int32.Parse(row["ThreeH"].ToString());
                int minute3 = Int32.Parse(row["ThreeM"].ToString());
                if (minute3 >= 60)
                {
                    int tmp = minute3 / 60;
                    hour3 += tmp;
                    minute3 -= 60 * tmp;
                }

                int hour2 = Int32.Parse(row["SecondH"].ToString());
                int minute2 = Int32.Parse(row["SecondM"].ToString());
                if (minute2 >= 60)
                {
                    int tmp = minute2 / 60;
                    hour2 += tmp;
                    minute2 -= 60 * tmp;
                }

                int hour1 = Int32.Parse(row["FirstH"].ToString());
                int minute1 = Int32.Parse(row["FirstM"].ToString());
                if (minute1 >= 60)
                {
                    int tmp = minute1 / 60;
                    hour1 += tmp;
                    minute1 -= 60 * tmp;
                }
                int hour = Int32.Parse(row["TodayH"].ToString());
                int minute = Int32.Parse(row["TodayM"].ToString());
                if (minute >= 60)
                {
                    int tmp = minute / 60;
                    hour += tmp;
                    minute -= 60 * tmp;
                }
                totalh += hour; totalm += minute;
                total1h += hour1; total1m += minute1;
                total2h += hour2; total2m += minute2;
                total3h += hour3; total3m += minute3;

                result += "<tr><td>" + row["TypeTimeName"].ToString() + "</td><td align='center'>" + hour3 + ":" + minute3 + "</td><td align='center'>" + hour2 + ":" + minute2 + "</td><td align='center'>" + hour1 + ":" + minute1 + "</td><td align='center'>" + hour + ":" + minute + "</td></tr>";
            }

            if (total3m >= 60)
            {
                int tmp = total3m / 60;
                total3h += tmp;
                total3m -= 60 * tmp;
            }
            if (total2m >= 60)
            {
                int tmp = total2m / 60;
                total2h += tmp;
                total2m -= 60 * tmp;
            }
            if (total1m >= 60)
            {
                int tmp = total1m / 60;
                total1h += tmp;
                total1m -= 60 * tmp;
            }
            if (totalm >= 60)
            {
                int tmp = totalm / 60;
                totalh += tmp;
                totalm -= 60 * tmp;
            }
            result += "<tr><td class='footer'>Tổng</td><td class='footer' align='center'>" + total3h + ":" + total3m + "</td><td class='footer' align='center'>" + total2h + ":" + total2m + "</td><td class='footer' align='center'>" + total1h + ":" + total1m + "</td><td class='footer' align='center'>" + totalh + ":" + totalm + "</td></tr>";
            result += "</table></tr></table>";
            return result;
        }

        public static string selReportedTimeActivity(DateTime date, string email, string companyid)
        {
            DaMain main = new DaMain();
            DataTable data = main.selReportedTimeActivity(date, email, companyid);
            string result = String.Empty;
            result += "<table>";
            result += "<tr><th class='header'>Thời gian công việc trong tháng</th><tr>";
            result += "<tr><table class='inbox'>";
            result += "<tr><th align='center' style='color:#375B91;background-color:#F3F9FF;'>Tên sản phẩm</th><th align='center' style='color:#375B91;background-color:#F3F9FF;'>Tên công việc</th><th align='center' style='color:#375B91;background-color:#F3F9FF;'>Loại thời giờ</th><th align='center' style='color:#375B91;background-color:#F3F9FF;'>Thời gian thực tế</th></tr>";

            int numOfRow = data.Rows.Count;
            DataRow row;
            int totalh = 0;
            int totalm = 0;

            for (int i = 0; i < numOfRow; i++)
            {
                row = data.Rows[i];
                
                int hour = Int32.Parse(row["Hour"].ToString());
                int minute = Int32.Parse(row["Minute"].ToString());
                if (minute >= 60)
                {
                    int tmp = minute / 60;
                    hour += tmp;
                    minute -= 60 * tmp;
                }

                totalh += hour; totalm += minute;

                result += "<tr><td>" + row["ProjectName"].ToString() + "</td><td align='left'>" + row["ActivityName"].ToString() + "</td><td align='left'>" + row["TypeTimeName"].ToString() + "</td><td align='center'>" + hour + ":" + minute + "</td></tr>";
            }

            if (totalm >= 60)
            {
                int tmp = totalm / 60;
                totalh += tmp;
                totalm -= 60 * tmp;
            }
            result += "<tr><td class='footer' align='center'>Tổng</td><td>&nbsp;</td><td>&nbsp;</td><td class='footer' align='center'>" + totalh + ":" + totalm + "</td></tr>";
            result += "</table></tr></table>";
            return result;
        }

        public static string selMyProject(string email, string companyid)
        {
            DaMain main = new DaMain();
            DataTable data = main.selMyProject(email, companyid);
            string result = String.Empty;
            result += "<table>";
            result += "<tr><th class='header'>Thời gian sản phẩm theo loại thời giờ</th><tr>";
            result += "<tr><table class='inbox'>";
            result += "<tr><th align='left' style='color:#375B91;background-color:#F3F9FF;'>ID</th><th align='center' style='color:#375B91;background-color:#F3F9FF;'>Tên sản phẩm</th><th align='center' style='color:#375B91;background-color:#F3F9FF;'>Giờ hành chính</th><th align='center' style='color:#375B91;background-color:#F3F9FF;'>Ngoài giờ</th><th align='center' style='color:#375B91;background-color:#F3F9FF;'>Ngày nghỉ (cuối tuần, lễ, tết)</th><th align='center' style='color:#375B91;background-color:#F3F9FF;' class='footer'>Tổng</th></tr>";

            int numOfRow = data.Rows.Count;
            DataRow row;

            for (int i = 0; i < numOfRow; i++)
            {
                int totalh = 0;
                int totalm = 0;

                row = data.Rows[i];
                int hour3 = Int32.Parse(row["ReH"].ToString());
                int minute3 = Int32.Parse(row["ReM"].ToString());
                if (minute3 >= 60)
                {
                    int tmp = minute3 / 60;
                    hour3 += tmp;
                    minute3 -= 60 * tmp;
                }

                int hour2 = Int32.Parse(row["BiH"].ToString());
                int minute2 = Int32.Parse(row["BiM"].ToString());
                if (minute2 >= 60)
                {
                    int tmp = minute2 / 60;
                    hour2 += tmp;
                    minute2 -= 60 * tmp;
                }

                int hour1 = Int32.Parse(row["HoH"].ToString());
                int minute1 = Int32.Parse(row["HoM"].ToString());
                if (minute1 >= 60)
                {
                    int tmp = minute1 / 60;
                    hour1 += tmp;
                    minute1 -= 60 * tmp;
                }
                int hour = Int32.Parse(row["SiH"].ToString());
                int minute = Int32.Parse(row["SiM"].ToString());
                if (minute >= 60)
                {
                    int tmp = minute / 60;
                    hour += tmp;
                    minute -= 60 * tmp;
                }
                totalh += (hour + hour1 + hour2 + hour3); totalm += minute + minute1 + minute2 + minute3;
                if (totalm >= 60)
                {
                    int tmp = totalm / 60;
                    totalh += tmp;
                    totalm -= 60 * tmp;
                }

                result += "<tr><td>" + row["ProjectID"].ToString() + "</td><td>" + row["ProjectName"].ToString() + "</td><td align='center'>" + hour3 + ":" + minute3 + "</td><td align='center'>" + hour2 + ":" + minute2 + "</td><td align='center'>" + hour1 + ":" + minute1 + "</td><td align='center' class='footer'>" + totalh + ":" + totalm + "</td></tr>";
            }

            result += "</table></tr></table>";
            return result;
        }

        public static string selWeekend(DateTime date, string email, string companyid)
        {
            DaMain main = new DaMain();
            DataTable data = main.selWeekend(date, email, companyid);
            string result = String.Empty;
            result += "<table>";
            result += "<tr><th class='header'>7 ngày gần đây</th><tr>";
            result += "<tr><table class='inbox'>";
            result += "<tr><th align='center' style='color:#375B91;background-color:#F3F9FF;'>Ngày</th><th align='center' style='color:#375B91;background-color:#F3F9FF;'>Thời gian</th></tr>";

            int numOfRow = data.Rows.Count;
            DataRow row;

            for (int i = 0; i < numOfRow; i++)
            {
                row = data.Rows[i];
                int hour3 = Int32.Parse(row["H3"].ToString());
                int minute3 = Int32.Parse(row["M3"].ToString());
                if (minute3 >= 60)
                {
                    int tmp = minute3 / 60;
                    hour3 += tmp;
                    minute3 -= 60 * tmp;
                }

                int hour2 = Int32.Parse(row["H2"].ToString());
                int minute2 = Int32.Parse(row["M2"].ToString());
                if (minute2 >= 60)
                {
                    int tmp = minute2 / 60;
                    hour2 += tmp;
                    minute2 -= 60 * tmp;
                }

                int hour1 = Int32.Parse(row["H1"].ToString());
                int minute1 = Int32.Parse(row["M1"].ToString());
                if (minute1 >= 60)
                {
                    int tmp = minute1 / 60;
                    hour1 += tmp;
                    minute1 -= 60 * tmp;
                }
                int hour4 = Int32.Parse(row["H4"].ToString());
                int minute4 = Int32.Parse(row["M4"].ToString());
                if (minute4 >= 60)
                {
                    int tmp = minute4 / 60;
                    hour4 += tmp;
                    minute4 -= 60 * tmp;
                }

                int hour5 = Int32.Parse(row["H5"].ToString());
                int minute5 = Int32.Parse(row["M5"].ToString());
                if (minute5 >= 60)
                {
                    int tmp = minute5 / 60;
                    hour5 += tmp;
                    minute5 -= 60 * tmp;
                }
                int hour6 = Int32.Parse(row["H6"].ToString());
                int minute6 = Int32.Parse(row["M6"].ToString());
                if (minute6 >= 60)
                {
                    int tmp = minute6 / 60;
                    hour6 += tmp;
                    minute6 -= 60 * tmp;
                }

                int hour7 = Int32.Parse(row["H7"].ToString());
                int minute7 = Int32.Parse(row["M7"].ToString());
                if (minute7 >= 60)
                {
                    int tmp = minute7 / 60;
                    hour7 += tmp;
                    minute7 -= 60 * tmp;
                }
                result += "<tr><td>" + GetDayOfWeek(DateTime.Now, 1).ToString("dddd, dd/MM/yyyy", new CultureInfo("vi-VN")) + "</td><td align='center'>" + hour1 + ":" + minute1 + "</td></tr>";
                result += "<tr><td>" + GetDayOfWeek(DateTime.Now, 2).ToString("dddd, dd/MM/yyyy", new CultureInfo("vi-VN")) + "</td><td align='center'>" + hour2 + ":" + minute2 + "</td></tr>";
                result += "<tr><td>" + GetDayOfWeek(DateTime.Now, 3).ToString("dddd, dd/MM/yyyy", new CultureInfo("vi-VN")) + "</td><td align='center'>" + hour3 + ":" + minute3 + "</td></tr>";
                result += "<tr><td>" + GetDayOfWeek(DateTime.Now, 4).ToString("dddd, dd/MM/yyyy", new CultureInfo("vi-VN")) + "</td><td align='center'>" + hour4 + ":" + minute4 + "</td></tr>";
                result += "<tr><td>" + GetDayOfWeek(DateTime.Now, 5).ToString("dddd, dd/MM/yyyy", new CultureInfo("vi-VN")) + "</td><td align='center'>" + hour5 + ":" + minute5 + "</td></tr>";
                result += "<tr><td>" + GetDayOfWeek(DateTime.Now, 6).ToString("dddd, dd/MM/yyyy", new CultureInfo("vi-VN")) + "</td><td align='center'>" + hour6 + ":" + minute6 + "</td></tr>";
                result += "<tr><td>" + GetDayOfWeek(DateTime.Now, 7).ToString("dddd, dd/MM/yyyy", new CultureInfo("vi-VN")) + "</td><td align='center'>" + hour7 + ":" + minute7 + "</td></tr>";
            }

            result += "</table></tr></table>";
            return result;
        }

        public static DateTime GetDayOfWeek(DateTime dt,int index)
        {
            return dt.AddDays(-index);
        }
    }
}
